﻿#include <stdio.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>


#define DEBUG
#define PORT 2345
struct epoll_event ev, events[500];

int analyse_request();

int matching_rule();

int build_connect();

int forward_request();

int echo_request();

char buf[BUFSIZ];

int main(int argc, char const **argv)
{
	char get[256] = { '\0' };
	bzero(buf, BUFSIZ);
	int efd = epoll_create1(0);
	if (efd<=0)
	{
		perror("epoll_create failed");
		_exit(-1);
	}
#ifndef DEBUG
	daemon(0, 0);
#endif // DEBUG
	int sockfd, newsockfd;
	struct sockaddr_in addr, serveraddr;
	int addr_len = sizeof(struct sockaddr_in);
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	bzero(&addr, sizeof(struct sockaddr_in));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(PORT);
	addr.sin_addr.s_addr - htonl(INADDR_ANY);
	if (sockfd)
	{
		bind(sockfd, (struct sockaddr *)(&addr), sizeof(struct sockaddr));
		listen(sockfd, 20);
		ev.data.fd = sockfd;
		ev.events = EPOLLIN;
		epoll_ctl(efd, EPOLL_CTL_ADD, ev.data.fd, &ev);
	}
	while (1)
	{
		int n = epoll_wait(efd, events, 500, 0);
		for (int i = 0; i<n; i++)
		{
			if (events[i].data.fd == sockfd)
			{
				int len = sizeof(serveraddr);
				int *cfd = calloc(1, sizeof(int));
				*cfd = accept(sockfd, (struct sockaddr *)&serveraddr, &len);
				ev.data.fd = cfd;
				ev.events = EPOLLIN;
				epoll_ctl(efd, EPOLL_CTL_ADD, *(int*)(ev.data.fd), &ev);
			}
			else if (events[i].events & EPOLLIN) //接收到数据，读socket
			{
				n = read(*(int*)(events[i].data.fd), buf, BUFSIZ);    //读
				printf("%s", buf);
				//分析请求
				sscanf(buf, "GET %s HTTP/1.1",get);
				printf("%s", get);
				bzero(buf, BUFSIZ);
				bzero(get, 256);
				write(*(int*)(events[i].data.fd), "HTTP/1.1 200 OK\n", 16);
				write(*(int*)(events[i].data.fd), "Content-Type: text/html;charset=utf-8\r\n\r\n", 41);       //发送数据
				write(*(int*)(events[i].data.fd), "<html><head></head><body>see me</body></html>", 46);
				//ev.events = EPOLLOUT | EPOLLET;
				close(*(int*)(events[i].data.fd));
				free(events[i].data.fd);
				epoll_ctl(efd, EPOLL_CTL_DEL, *(int*)(events[i].data.fd), &ev);//修改标识符，等待下一个循环时发送数据，异步处理的精髓
			}
			//if (events[i].events & EPOLLOUT) //有数据待发送，写socket
			//{
			//	ev.data.fd = events[i].data.fd;
			//	ev.events = EPOLLIN | EPOLLET;
			//	epoll_ctl(efd, EPOLL_CTL_MOD, *(int*)(events[i].data.fd), &ev); //修改标识符，等待下一个循环时接收数据
			//}
			//else
			//{
			//	/*close(*(int*)(events[i].data.fd));
			//	free(events[i].data.fd);
			//	epoll_ctl(efd, EPOLL_CTL_DEL, *(int*)(events[i].data.fd), &ev);*/
			//}
		}
	}
	close(efd);
	close(sockfd);

	return 0;
}

int analyse_request()
{
	return 0;
}

int matching_rule()
{
	return 0;
}

int build_connect()
{
	return 0;
}

int forward_request()
{
	return 0;
}

int echo_request()
{
	return 0;
}